﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace HKTools
{
    public class HK_MenuData
    {
        public string Name { get; set; }

        public int Level { get; set; }

        public HK_MenuData(string name, int level)
        {
            Name = name;
            Level = level;
        }
    }

    public class HK_MenuItem : HK_CollapsibleMenuController
    {
        HK_MenuData menuData;

        // 箭头按钮
        [Header("菜单按钮："), SerializeField] Button menu_Btn;
        [Header("菜单文本："), SerializeField] Text menu_Txt;
        [Header("按钮图片："), SerializeField] Image arrow_Img;

        [SerializeField] Sprite down, right, dot;

        List<HK_MenuItem> childList;

        // 子物体开启状态
        [SerializeField] bool isShowChild;

        // 起始大小
        Vector2 startSize;
        RectTransform rectTrans;

        void Awake()
        {
            childList = new List<HK_MenuItem>();

            rectTrans = GetComponent<RectTransform>();

            menu_Btn.onClick.AddListener(() =>
            {
                if (isShowChild)
                    CloseChild();
                else
                    OpenChild();

                isShowChild = !isShowChild;

                this.SendCommand(new CMD_MenuButtonClick(menuData));
            });

            arrow_Img.sprite = dot;

            startSize = rectTrans.sizeDelta;
            isShowChild = false;
        }

        /// <summary>
        /// 填充Item数据
        /// </summary>
        /// <param name="menuList"></param>
        public void InitPanelContent(HK_MenuData menuData)
        {
            menu_Txt.text = menuData.Name;

            this.menuData = menuData;
        }

        public void SetParent(Transform parent)
        {
            transform.parent = parent;
        }

        public void SetParent(HK_MenuItem menuList)
        {
            transform.parent = menuList.transform;
            menuList.AddChild(this);
            GetComponent<VerticalLayoutGroup>().padding = new RectOffset((int)menuList.arrow_Img.GetComponent<RectTransform>().sizeDelta.x, 0, 0, 0);
            if (menuList.isShowChild)
            {
                GetComponent<HK_MenuItem>().AddParentSize((int)gameObject.GetComponent<RectTransform>().sizeDelta.y);
            }
            else
            {
                gameObject.SetActive(false);
            }
        }

        //添加子物体到集合
        private void AddChild(HK_MenuItem menuList)
        {
            childList.Add(menuList);
            if (childList.Count >= 1)
            {
                arrow_Img.sprite = right;
            }
        }

        void CloseChild()
        {
            if (childList.Count == 0)
                return;

            foreach (HK_MenuItem child in childList)
            {
                child.gameObject.SetActive(false);
                child.GetComponent<HK_MenuItem>().AddParentSize(-(int)child.GetComponent<RectTransform>().sizeDelta.y);
            }
            arrow_Img.sprite = right;
        }

        void OpenChild()
        {
            if (childList.Count == 0)
                return;

            foreach (HK_MenuItem child in childList)
            {
                child.gameObject.SetActive(true);
                child.GetComponent<HK_MenuItem>().AddParentSize((int)child.GetComponent<RectTransform>().sizeDelta.y);
            }
            arrow_Img.sprite = down;
        }

        /// <summary>
        /// 增加父物体高度
        /// </summary>
        /// <param name="change"></param>
        public void AddParentSize(int change)
        {
            if (transform.parent.GetComponent<HK_MenuItem>() != null)
            {
                transform.parent.GetComponent<HK_MenuItem>().UpdateRectTranSize(change);
                transform.parent.GetComponent<HK_MenuItem>().AddParentSize(change);
            }
        }

        /// <summary>
        /// 增加一个子物体后增加Panel大小
        /// </summary>
        /// <param name="change"></param>
        public void UpdateRectTranSize(int change)
        {
            RectTransform rectTran = gameObject.GetComponent<RectTransform>();

            rectTran.sizeDelta = new Vector2(startSize.x, rectTran.sizeDelta.y + change);
        }
    }
}